<template>
  <div class="building-leader-wrapper">
    <p class="title">综合条件查询</p>
    <div class="form-search-area">
      <el-form :inline="true" :model="formInline" size="medium" class="demo-form-inline">
        <el-form-item label="小区（单位）名称">
          <el-input v-model="formInline.villageNameLike" placeholder="请输入小区（单位）名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="楼栋长姓名">
          <el-input v-model="formInline.buildingLeaderNameLike" placeholder="请输入楼栋长姓名" clearable></el-input>
        </el-form-item>
        <el-form-item label="联系方式">
          <el-input v-model="formInline.cellphoneLike" placeholder="请输入联系方式" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="medium" @click="searchOrg">查询</el-button>
          <el-button size="medium" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="right-btn-area">
        <el-button size="medium" type="primary" @click="popNewAddDialog">新增楼栋长</el-button>
        <el-button size="medium">导入</el-button>
        <el-button size="medium">导出</el-button>
        <el-button size="medium" @click="batchDeletion">批量删除</el-button>
      </div>
    </div>
    <!-- 表格 开始-->
    <div class="table-area clearfix">
      <el-table :data="tableData" ref="multipleTable" @selection-change="selectionChange" v-loading="loading" stripe>
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="序号" width="50">
          <template slot-scope="scope">{{ scope.$index + 1 }}</template>
        </el-table-column>
        <el-table-column label="小区（单位）名称" prop="villageName"></el-table-column>
        <el-table-column label="楼栋名称" prop="buildingName"></el-table-column>
        <el-table-column label="楼栋长姓名" prop="buildingLeaderName"></el-table-column>
        <el-table-column label="性别" prop="sex"></el-table-column>
        <el-table-column label="联系方式" prop="cellphone"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-link type="primary" :underline="false" icon="el-icon-view" @click="viewDetail(scope.row.id)">查看</el-link>
            <el-link type="success" :underline="false" icon="el-icon-edit" @click="editDetail(scope.row.id)">编辑</el-link>
            <el-link type="danger" :underline="false" icon="el-icon-delete" @click="deleteDetail(scope.row.id)">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="paging-self"
        background
        :current-page.sync="currentPage"
        @current-change="pageChange"
        layout="total, prev, pager, next"
        :total="totalCount">
      </el-pagination>
    </div>
    <!-- 表格 结束-->
    <!-- 对话框 开始 -->
    <el-dialog title="楼栋长" :visible.sync="dialogFormVisible" width="810px" @closed="closeDialog">
      <el-form :model="dialogForm" class="dialog-form-comot" :disabled="dialogMode===CNST.READ_MODE" :rules="rules" ref="ruleDialog">
        <el-row>
          <el-col :span="12">
            <el-form-item label="小区(单位)名称" label-width="116px" prop="villageName">
              <el-input v-model="dialogForm.villageName" placeholder="请输入小区(单位)名称" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="楼栋名称" label-width="116px" prop="buildingName">
              <el-input v-model="dialogForm.buildingName" placeholder="请输入楼栋名称" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="建筑面积" label-width="116px" prop="buildingArea">
              <el-input v-model="dialogForm.buildingArea" placeholder="请输入建筑面积" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="层数" label-width="116px" prop="layerNum">
              <el-input v-model="dialogForm.layerNum" placeholder="请输入层数" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="单元数" label-width="116px" prop="buildingUnitNum">
              <el-input v-model="dialogForm.buildingUnitNum" placeholder="请输入单元数" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="楼栋户数" label-width="116px" prop="buildingHouseNum">
              <el-input v-model="dialogForm.buildingHouseNum" placeholder="请输入楼栋户数" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="楼栋人数" label-width="116px" prop="buildingPeopleNum">
              <el-input v-model="dialogForm.buildingPeopleNum" placeholder="请输入楼栋人数" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="楼栋长姓名" label-width="116px" prop="buildingLeaderName">
              <el-input v-model="dialogForm.buildingLeaderName" placeholder="请输入楼栋长姓名" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="性别" label-width="116px" prop="sex">
              <el-select v-model="dialogForm.sex" placeholder="请选择性别" clearable>
                <el-option label="男" value="男"></el-option>
                <el-option label="女" value="女"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="民族" label-width="116px" prop="nation">
              <el-select v-model="dialogForm.nation" placeholder="请选择民族" clearable>
                <el-option
                 v-for="(item) in DICY.nation"
                 :key="item.code"
                 :label="item.name"
                 :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="政治面貌" label-width="116px" prop="politicalStatus">
              <el-select v-model="dialogForm.politicalStatus" placeholder="请选择政治面貌" clearable>
                <el-option
                 v-for="(item, index) in DICY.politicalStatus"
                 :key="index"
                 :label="item.name"
                 :value="item.name">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出生日期" label-width="116px" prop="birthDate">
              <el-date-picker
                v-model="dialogForm.birthDate"
                type="date"
                placeholder="请选择选择出生日期" clearable>
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="学历" label-width="116px" prop="education">
              <el-select v-model="dialogForm.education" placeholder="请选择学历" clearable>
                <el-option
                 v-for="(item) in DICY.education"
                 :key="item.code"
                 :label="item.name"
                 :value="item.code">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号码" label-width="116px" prop="cellphone">
              <el-input v-model="dialogForm.cellphone" placeholder="请输入手机号码" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="固定电话" label-width="116px" prop="fixedTelephone">
              <el-input v-model="dialogForm.fixedTelephone" placeholder="请输入固定电话" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所在地" label-width="116px" prop="location">
              <el-cascader
                v-model="dialogForm.location"
                :options="options"
                @change="handleChange"
                placeholder="请选择所在地">
              </el-cascader>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="详细地址" label-width="116px" prop="locationAddress">
              <el-input type="textarea" v-model="dialogForm.locationAddress" :rows="2" placeholder="请输入详细地址" autocomplete="off" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="经度" label-width="116px" prop="longitude">
              <el-input v-model="dialogForm.longitude" placeholder="请输入经度"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬度" label-width="116px" prop="latitude">
              <el-input v-model="dialogForm.latitude" placeholder="请输入纬度"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitOrg" v-if="dialogMode!==CNST.READ_MODE">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 对话框 结束 -->
  </div>
</template>

<script>
import {
  getGovernBuildingLeaderList,
  addGovernBuildingLeader,
  getGovernBuildingLeaderDetail,
  updateGovernBuildingLeaderDetail,
  deleteGovernBuildingLeader,
  deleteGovernBuildingLeaderBatch
} from '@/api/baseBusiness'
import { mapState } from 'vuex'
import { deepClone } from '@/utils'
export default {
  name: 'BuildingLeader',
  data () {
    return {
      formInline: {
        villageNameLike: '',
        buildingLeaderNameLike: '',
        cellphoneLike: ''
      },
      loading: false,
      tableData: [],
      multipleSelection: [],
      currentPage: 1,
      totalCount: 0,
      dialogFormVisible: false,
      dialogForm: {
        villageName: '',
        buildingName: '',
        buildingArea: '',
        layerNum: '',
        buildingUnitNum: '',
        buildingHouseNum: '',
        buildingPeopleNum: '',
        buildingLeaderName: '',
        sex: '',
        nation: '',
        politicalStatus: '',
        birthDate: '',
        education: '',
        cellphone: '',
        fixedTelephone: '',
        location: '',
        locationAddress: '',
        longitude: '',
        latitude: ''
      },
      rules: {
        villageName: [
          { required: true, message: '请输入小区(单位)名称', trigger: 'blur' },
          { max: 100, message: '长度在 50 个汉字以内', trigger: 'blur' }
        ],
        buildingName: [
          { required: true, message: '请输入楼栋名称', trigger: 'blur' }
        ],
        layerNum: [
          { required: true, message: '请输入层数', trigger: 'blur' }
        ],
        buildingUnitNum: [
          { required: true, message: '请输入单元数', trigger: 'blur' }
        ],
        buildingLeaderName: [
          { required: true, message: '请输入楼栋长姓名', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        nation: [
          { required: true, message: '请选择民族', trigger: 'change' }
        ],
        politicalStatus: [
          { required: true, message: '请选择政治面貌', trigger: 'change' }
        ],
        birthDate: [],
        education: [
          { required: true, message: '请选择学历', trigger: 'change' }
        ],
        cellphone: [
          { required: true, message: '请输入手机号码', trigger: 'blur' }
        ],
        location: [
          // { required: true, message: '请选择所在地', trigger: 'change' }
        ]
      },
      dialogMode: '', // 对话框的状态，新增，查看，编辑
      curId: '', // 点击查看或编辑的当前列表项的id
      options: this.CNST.jilian
    }
  },
  computed: {
    ...mapState([
      'areaId'
    ])
  },
  methods: {
    searchOrg () {
      this.currentPage = 1
      this.getOrgListAndShow()
    },
    resetSearch () {
      for (let key in this.formInline) {
        this.$set(this.formInline, key, '')
      }
      this.currentPage = 1
      this.getOrgListAndShow()
    },
    popNewAddDialog () {
      this.dialogFormVisible = true
      this.dialogMode = this.CNST.ADDNEW_MODE
    },
    batchDeletion () {
      if (this.multipleSelection.length === 0) {
        this.$message({
          type: 'warning',
          message: '请勾选要删除的项!'
        })
      } else {
        var ids = this.multipleSelection.map((item) => {
          return item.id
        }).join(',')
        this.$confirm('确定删除, 是否继续?', '批量删除提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteGovernBuildingLeaderBatch({ ids }).then((res) => {
            if (res.code === '200000') {
              this.$message({
                type: 'success',
                message: '批量删除成功!'
              })
              this.getOrgListAndShow()
            }
          })
        })
      }
    },
    selectionChange (valArr) {
      this.multipleSelection = valArr
    },
    viewDetail (id) {
      this.dialogMode = this.CNST.READ_MODE
      this.dialogFormVisible = true
      getGovernBuildingLeaderDetail({ id }).then((res) => {
        this.showToDialog(res.data)
      })
    },
    editDetail (id) {
      this.dialogMode = this.CNST.EDIT_MODE
      this.dialogFormVisible = true
      this.curId = id
      getGovernBuildingLeaderDetail({ id }).then((res) => {
        this.showToDialog(res.data)
      })
    },
    deleteDetail (id) {
      this.$confirm('确定删除, 是否继续?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then((res) => {
        deleteGovernBuildingLeader({ id }).then((res) => {
          if (res.code === '200000') {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getOrgListAndShow()
          }
        })
      })
    },
    pageChange (curPage) {
      this.currentPage = curPage
      this.getOrgListAndShow()
    },
    showToDialog (data) {
      for (let key in data) {
        if (this.dialogForm.hasOwnProperty(key)) {
          this.$set(this.dialogForm, key, data[key])
        }
      }
    },
    submitOrg () {
      this.$refs.ruleDialog.validate((valid) => {
        if (valid) {
          // 验证通过
          let requestData = deepClone(this.dialogForm)
          if (this.dialogMode === this.CNST.ADDNEW_MODE) {
            // 新增
            requestData.areaId = this.areaId
            addGovernBuildingLeader(requestData).then((res) => {
              if (res.code === '200000') {
                this.$message({
                  message: '新增成功',
                  type: 'success'
                })
                this.getOrgListAndShow()
                this.dialogFormVisible = false
              }
            })
          } else if (this.dialogMode === this.CNST.EDIT_MODE) {
            // 修改
            requestData.id = this.curId
            updateGovernBuildingLeaderDetail(requestData).then((res) => {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.getOrgListAndShow()
              this.dialogFormVisible = false
            })
          }
        } else {
          return false
        }
      })
    },
    async getOrgListAndShow () {
      try {
        this.loading = true
        let requestData = {
          page: this.currentPage,
          areaId: this.areaId,
          ...deepClone(this.formInline)
        }
        var res = await getGovernBuildingLeaderList(requestData)
        this.tableData = res.data.queryResult
        this.totalCount = res.data.totalCount
      } catch (error) {
        console.error(error)
      } finally {
        this.loading = false
      }
    },
    closeDialog () {
      // 当前点击查看或编辑列表项的id置空
      this.curId = ''
      // 弹框关闭后重置表单验证
      this.$refs.ruleDialog.resetFields()
      // 清空弹框内表单数据
      for (let key in this.dialogForm) {
        this.dialogForm[key] = ''
      }
    },
    handleChange (value) {
      console.log(value)
    }
  },
  mounted () {
    this.getOrgListAndShow()
  }
}
</script>

<style lang="scss" scoped>
.building-leader-wrapper {
  background-color: #ffffff;
  margin-top: 12px;
  border-radius: 4px;
  box-shadow: 10px 10px 22px -2px #d2d6da;
  padding: 20px;
  .title {
    margin: 0;
    font-size: 15px;
    color: #333333;
    margin-bottom: 10px;
  }
  .form-search-area {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
  }
  .table-area {
    .el-link {
      margin-right: 13px;
    }
    .el-pagination.paging-self {
      margin-top: 20px;
      float: right;
    }
  }
  .el-form.dialog-form-comot {
    .el-form-item {
      .el-cascader,.el-select,.el-input-number {
        width: 100%;
      }
      .el-date-editor.el-input,.el-date-editor.el-input__inner {
        width: 100%;
      }
    }
  }
}
</style>
